<?php

//认证支付 -连连支付  http://open.lianlianpay.com/#cat=71
namespace app\index\controller;
use think\Model;
use think\Db;

use think\Log;

require_once (dirname(__FILE__)."/../../llpay_notify/llpay.config.php");
require_once (dirname(__FILE__)."/../../llpay_notify/lib/llpay_notify.class.php");

//http://open.lianlianpay.com/#cat=113 连连支付文档

class Llpay{

    public function index(){
        //计算得出通知验证结果
        global $llpay_config;

        $llpayNotify = new \LLpayNotify($llpay_config);
        $llpayNotify->verifyNotify();
        if ($llpayNotify->result) { //验证成功
            //获取连连支付的通知返回参数，可参考技术文档中服务器异步通知参数列表
            $no_order = $llpayNotify->notifyResp['no_order'];//商户订单号
            $oid_paybill = $llpayNotify->notifyResp['oid_paybill'];//连连支付单号
            $result_pay = $llpayNotify->notifyResp['result_pay'];//支付结果，SUCCESS：为支付成功
            $money_order = $llpayNotify->notifyResp['money_order'];// 支付金额
            if($result_pay == "SUCCESS"){
                //请在这里加上商户的业务逻辑程序代(更新订单状态、入账业务)
                //——请根据您的业务逻辑来编写程序——
                //payAfter($llpayNotify->notifyResp);
                
                // 修改提现记录状态
                $no_order = substr($no_order, 10);

                Db::table("xh_member_withdraw")->where('id', $no_order)->setField('status', 3);
                $memberId = Db::table("xh_member_withdraw")->where('id', $no_order)->value('memberId');
                $usableSum = Db::table("xh_member")->where('id', $memberId)->value('usableSum');

                //增加资金记录
                $data=array();
                $data['memberId'] = $memberId;
                $data['flow'] = 2;
                $data['amount'] = $money_order;
                $data['usableSum'] = $usableSum;
                $data['remarks'] = "提现申请审核通过，扣除{$money_order}元";
                $data['createTime'] = date("Y-m-d H:i:s");
                $id = Db::table("xh_member_fundrecord")->insertGetId($data);

            }
            file_put_contents("log.txt", "异步通知 验证成功\n", FILE_APPEND);
            die("{'ret_code':'0000','ret_msg':'交易成功'}"); //请不要修改或删除
            /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        } else {
            file_put_contents("log.txt", "异步通知 验证失败\n", FILE_APPEND);
            //验证失败
            die("{'ret_code':'9999','ret_msg':'验签失败'}");
            //调试用，写文本函数记录程序运行情况是否正常
            //logResult("这里写入想要调试的代码变量值，或其他运行的结果记录");
        }
    }

}